﻿using MyHostSwtich.Core;
using MyHostSwtich.Datas.Models;

using System;
using System.Threading.Tasks;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;

namespace MyHostSwtich
{
    public partial class frmMain : DockContent, IMainForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void mnuHideMain_Click(object sender, EventArgs e)
        {
            ShowIt(false);
        }

        private void mnuShowMain_Click(object sender, EventArgs e)
        {
            ShowIt(true);
        }

        private void ShowIt(bool value = true)
        {
            this.mnuHideMain.Visible = value;
            this.mnuShowMain.Visible = !value;
            if (value)
            {
                this.Show();
            }
            else
            {
                this.Hide();
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            DoReady();
            Init();
        }

        private void Init()
        {
            InitConfigList();
        }

        private void InitConfigList()
        {
            var frm = new frmConfigList()
            {
                MdiParent = this,
                OwnerParent = this,
                DockPanel = this.dpMain,
                DockState = DockState.DockLeft
            };
            frm.Show();
        }

        private async void DoReady()
        {
            await Task.Delay(100);
            this.ShowIt(false);
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            switch (this.WindowState)
            {
                case FormWindowState.Minimized: ShowIt(false); break;
                case FormWindowState.Normal:
                case FormWindowState.Maximized:
                    {
                        ShowIt(true);
                        break;
                    }
            }
        }

        private void Tray_DoubleClick(object sender, EventArgs e)
        {
            if (this.Visible == false)
            {
                ShowIt(true);
            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                ShowIt(false);
                e.Cancel = true;
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        void  IMainForm.ViewConfigItem(ConfigItem item)
        {
            foreach(Form fm in this.MdiChildren)
            {
                if (!(fm is IMdiItemForm ifrm)) continue;
                if (ifrm.Context.Key == item.Key)
                {
                    fm.Activate();
                    return;
                }
            }
            var frm = new frmConfigItem()
            {
                MdiParent = this,
                Context = item
            };
            frm.Text = item.Key;
            frm.DockPanel = this.dpMain;
            frm.Show();
        }

    }

}
